package com.wl.api.controller.system;

import java.util.function.Predicate;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.wl.api.common.utils.CollectionUtil;
import com.wl.api.common.utils.ErrorCodeEnum;
import com.wl.api.common.utils.JsonObject;
import com.wl.api.common.utils.ObjectUtil;
import com.wl.api.common.utils.StringUtil;
import com.wl.api.controller.base.CommonController;
import com.wl.api.dto.base.CommonUserDTO;
import com.wl.api.dto.system.SecurityChannelDTO;
import com.wl.api.model.system.SecurityChannel;
import com.wl.api.service.system.ISecurityChannelService;

/**
 *  channel,  ladder price 一起设置 
 *  渠道
 *  目前需求为一个渠道一个物流商 A area -> B area 一套物流 物流阶梯
 * 	每个国家只有一套取件阶梯和派件阶梯价
 * 
 *  后期 建议渠道改为产品包
 * @author chenj
   @desc 
 *
 */
@RestController
@RequestMapping("/admin/logistics/channel")
public class AdminChannelController extends CommonController {

	@Autowired
	ISecurityChannelService channelService;
	
	@RequestMapping(value="/manage", method=RequestMethod.POST)
	public JsonObject create(@RequestBody SecurityChannelDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "", code = "";
		if (validate().or(s -> s.getId() != null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
		if(pass) {
			initCreateUser(param);
			json.setData(channelService.insertChannelLogistics(param));
		}
		setJsonMessage(json, title, code, pass);
		return json;
	}
	/**
	 *  修改渠道数据数据
	 * @param param
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/manage", method = RequestMethod.PUT)
	public JsonObject modify(@RequestBody SecurityChannelDTO param) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		if (validate().or(s -> s.getId() == null).test(param)) {
			title = ErrorCodeEnum.param_error.getMessage();
			error_code = ErrorCodeEnum.param_error.getError_code();
			pass = false;
		}
    	//业务修改上架的商品也可以直接修改,同时商品状态不变  		// 不能修改上架数据
		/*if(pass && !ObjectUtil.isEmpty(channelService.selectOne(new EntityWrapper<SecurityChannel>(SecurityChannel.builder().status(1).id(param.getId()).build())))) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}*/
		if(pass) {
			initUpdateUser(param);
			channelService.updateChannelLogistics(param);
			json.setData(param.getId());
			
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	/** 只提供修改数据状态 */
	@RequestMapping(value = "/manage/{id}", method = RequestMethod.PUT)
	public JsonObject modifyStatus(@PathVariable("id")Long id, Integer status) throws Exception {
		JsonObject json = JsonObject.getInstance();
		CommonUserDTO user = getCommonUser();
		channelService.updateById(SecurityChannel.builder().id(id).updateUser(user.getUsername()).updateUserId(user.getId()).build());
		setJsonMessage(json);
		return json;
	}
	
	 @RequestMapping(value="/manage/{id}", method = RequestMethod.DELETE)
	public JsonObject del(@PathVariable("id") Long id) throws Exception {
		JsonObject json = JsonObject.getInstance();
		boolean pass = true;
		String title = "" , error_code = ""; 
		
		if(channelService.selectOne(new EntityWrapper<SecurityChannel>(SecurityChannel.builder().status(1).id(id).build())) == null) {
			error_code = ErrorCodeEnum.bad_request_published_data.getError_code();
			title = ErrorCodeEnum.bad_request_published_data.getMessage();
			pass = false;
		}
		if (pass) {
			channelService.deleteById(id);
		}
		setJsonMessage(json, title, error_code, pass);
		return json;
	}
	 @RequestMapping(value="/manage/{id}", method = RequestMethod.GET)
	 public JsonObject getDetail(@PathVariable("id") Long id){
		 JsonObject json = JsonObject.getInstance();
		 json.setData(channelService.getDetail(SecurityChannelDTO.builder().id(id).build()));
		 setJsonMessage(json);
		 return json;
	 }
	 
	 @RequestMapping(value="/manage", method = RequestMethod.GET)
	 public JsonObject getItemList(SecurityChannelDTO param){
		 JsonObject json = JsonObject.getInstance();
		 json.setPage(channelService.getItemList(param));
		 setJsonMessage(json);
		 return json;
	 }
	 
	 
	private void initCreateUser(SecurityChannelDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		//param.setStatus(0);
		param.buildCreateMsg(user.getId());
		param.buildUpdateMsg(user.getId(),user.getUsername());
	}
	private void initUpdateUser(SecurityChannelDTO param) throws Exception {
		CommonUserDTO user = getCommonUser();
		param.buildUpdateMsg(user.getUsername());
		
	}
	private Predicate<SecurityChannelDTO> validate() {
		return  s -> StringUtil.isEmpty(s.getName()) || StringUtil.isEmpty(s.getLogoImg())
				|| StringUtil.isEmpty(s.getChannelDesc()) || ObjectUtil.isEmpty(s.getCostTimeFrom()) ||
				ObjectUtil.isEmpty(s.getReachAreaId()) || ObjectUtil.isEmpty(s.getFromAreaId()) ||
				ObjectUtil.isEmpty(s.getCostTimeTo()) || CollectionUtil.isEmpty(s.getFeeLadderList()) ||
				s.getFeeLadderList().parallelStream().anyMatch(a -> ObjectUtil.isEmpty(a.getAmount())  || ObjectUtil.isEmpty(a.getWeightFrom()) || ObjectUtil.isEmpty(a.getWeightTo()));
	}
}
